#!/usr/local/env python3
#author:fonzie

import logging
from config import log
import os

def console_out(logFilename):
    ''' Output log to file and console '''
    # Define a Handler and set a format which output to file
    log_dir = log.get('path', None)
    if not log_dir:
        log_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs')
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    logFilename = os.path.join(log_dir, logFilename)

    logging.basicConfig(
                    level = logging.DEBUG,              # 定义输出到文件的log级别，
                    format = '%(asctime)s  %(filename)s : %(levelname)s  %(message)s',    # 定义输出log的格式
                    datefmt  = '%Y-%m-%d %A %H:%M:%S',                                     # 时间
                    filename = logFilename,                # log文件名
                    filemode = 'w')                        # 写入模式“w”或“a”
    # Define a Handler and set a format which output to console
    console = logging.StreamHandler()                  # 定义console handler
    console.setLevel(logging.INFO)                     # 定义该handler级别
    formatter = logging.Formatter('%(asctime)s  %(filename)s : %(levelname)s  %(message)s')  #定义该handler格式
    console.setFormatter(formatter)
    # Create an instance
    logging.getLogger().addHandler(console)           # 实例化添加handler

    return logging
